package com.he.architecture.service;

import com.he.architecture.Enums.CustomerOrderTypeEnums;
import com.he.architecture.common.service.LogicService;
import com.he.architecture.dao.CustomerDao;
import com.he.architecture.dao.ShopDao;
import com.he.architecture.entity.Customer;
import com.he.architecture.entity.CustomerOrder;
import com.he.architecture.entity.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;


@Service
public class CustomerService extends LogicService<CustomerDao, Customer,Long> {
    @Resource
    private ShopDao shopDao;
    /**
     * 默认构造器
     */
    public CustomerService(@Autowired CustomerDao cd) {
        super(cd);
    }

    public CustomerOrder formOrder(Long cus_id,Long shop_id,CustomerOrder customerOrder){
        //获取用户
        Customer customer = getDAO().getReferenceById(cus_id);

        //获取门店
        Shop shop=shopDao.getReferenceById(shop_id);
        //顾客创建订单
        CustomerOrder order=customer.formOrder(customerOrder);
        //订单关联用户，门店
        customerOrder.setCustomer(customer);
        customerOrder.setShop(shop);
        //通知门店,门店信息持久化
        shop.getCustomerOrders().add(customerOrder);
        shopDao.save(shop);
        getDAO().save(customer);
        return order;
    }

    /**
     * 顾客支付订单
     * @param cus_id 顾客id
     * @param shop_id 门店id
     * @param customerOrder 订单详情
     * @return 修改后订单
     */
    public Set<CustomerOrder> customerOrderTypeChange(Long cus_id,Long shop_id,CustomerOrder customerOrder,CustomerOrderTypeEnums enums){
        //获取顾客
        Customer customer = getDAO().getReferenceById(cus_id);

        //获取门店
        Shop shop=shopDao.getReferenceById(shop_id);
        //顾客订单
        Set<CustomerOrder> orders=customer.orderTypeChang(customerOrder, enums);
        //订单关联用户，门店
        customerOrder.setCustomer(customer);
        customerOrder.setShop(shop);
        //更新用户信息，应该会自动更新订单，商家状态
        getDAO().save(customer);
        return orders;
    }


}
